package com.zjitc.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjitc.common.ServerResponse;
import com.zjitc.common.redis.MyRedisPool;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;

import com.zjitc.utils.Conster;
import com.zjitc.utils.Question;
import com.zjitc.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/27 16:01
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

  private final String USERNAME = "username";
  private final String Email = "email";

  @Autowired
  private MyRedisPool redis;


  @Autowired
  private UserDao dao;

  @Override
  public ServerResponse<User> login(String username, String password) {
    User user = dao.login(username);
    if (user == null) {
      return ServerResponse.createError(" 用户名不存在");
    }
    if (!user.getPassword().equals(Utils.md5Password(password))) {
      return ServerResponse.createError(" 密码错误");
    }
    //清空 密码  问题和答案
    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> register(User user) {
    if (user.getUsername().equals(user.getEmail())) {
      return ServerResponse.createError("用户名与邮箱不可重名");
    }

    if (redis.existsSet(USERNAME, user.getUsername())) {
      return ServerResponse.createError("用户名已存在");
    }

    if (redis.existsSet(Email, user.getEmail())) {
      return ServerResponse.createError("邮箱已注册");
    }
    user.setPassword(Utils.md5Password(user.getPassword()));
    int i = dao.addUser(user);
    //Utils.setjedis(dao, redis);
    return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse<User> checkValid(String str, String type) {
    if(!redis.exists(type)){
      return ServerResponse.createError("对应type 不正确");
    }
    if (redis.existsSet(type, str)) {
      return ServerResponse.createError("用户名已存在");
    }
    return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse<User> forgetPassword(String username) {
    User checkname = dao.checkname(username);
    if (checkname == null) {
      return ServerResponse.createError("该用户不存在");
    }
    if (checkname.getQuestion() == null) {
      return ServerResponse.createError("该用户未设置找回密码问题");
    }

    return ServerResponse.createSuccess(new Question(checkname.getQuestion()));
  }

  @Override
  public ServerResponse<User> getInformation(User user) {
    if (user == null) {
      return ServerResponse.createinfomationError("用户未登录,无法获取当前用户信息");
    }
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse updateInformation(String email, String phone,
      String question, String answer, User user) {
    if (user == null) {
      return ServerResponse.createError("用户未登录");
    }
    User user1 = dao.findbyUsername(user.getUsername());
    user1.setEmail(email);
    user1.setPhone(phone);
    user1.setQuestion(question);
    user1.setAnswer(answer);

    dao.updateuser(user1);
    return ServerResponse.createSuccess("更新个人信息成功");
  }

  @Override
  public ServerResponse resetPassword(String passwordOld, String passwordNew, HttpSession session) {
    User seuser = (User) session.getAttribute(Conster.USER);
    if (seuser == null) {
      return ServerResponse.createError("用户未登录");
    }
    User user = dao.findbyUsername(seuser.getUsername());
    if (user.getPassword().equals(Utils.md5Password(passwordOld))) {
      return ServerResponse.createError("旧密码输入错误");
    }
    user.setPassword(Utils.md5Password(passwordNew));
    dao.updateuser(user);
    return ServerResponse.createSuccess("修改密码成功");
  }

  @Override
  public ServerResponse<User> forgetCheckAnswer(String username, String question, String answer) {
    User user = dao.findbyUsername(username);
    if (!user.getAnswer().equals(answer)) {
      return ServerResponse.createError("问题答案错误");
    }
    //创建一个token
    String token = Utils.getUUid();
    //放入缓存中
    redis.set(username, token);
    redis.expire(username, 600);
    return ServerResponse.createSuccess(token);
  }

  @Override
  public ServerResponse<User> forgetResetPassword(String username, String passwordNew,
      String forgetToken) {

    boolean exists = redis.exists(username);
    if (exists) {
      User user = dao.findbyUsername(username);
      try {
        user.setPassword(Utils.md5Password(passwordNew));
        dao.updateuser(user);
        redis.del(username);
        return ServerResponse.createSuccess("修改密码成功");
      } catch (Exception e) {
        e.printStackTrace();
        return ServerResponse.createError("修改密码错误");
      }
    }
    return ServerResponse.createError("token 已失效");
  }

  //后台管理员登陆
  @Override
  public ServerResponse<User> backLogin(String username, String password, HttpSession session) {
    User user = dao.findbyUsername(username);
    if (user.getRole() == 0) {
      if(user.getPassword().equals(Utils.md5Password(password))) {
        session.setAttribute(Conster.BACKUSER, user);
        return ServerResponse.createSuccess(user);
      }
      return ServerResponse.createError("密码错误");
    }
    return ServerResponse.createError("用户无权限");
  }

  @Override
  public ServerResponse<User> list(Integer pageNum, Integer pageSize, HttpSession session) {
    Object user = session.getAttribute(Conster.BACKUSER);
    if (user == null) {
      return ServerResponse.createBackError("用户未登录，请登录");
    }
    try {
      PageHelper.startPage(pageNum, pageSize);
      //
      List<User> userList = dao.findAll();
      PageInfo pageInfo = new PageInfo(userList);
      return ServerResponse.createSuccess(pageInfo);
    } catch (Exception e) {
      e.printStackTrace();
      return ServerResponse.createError("查询出错");
    }

  }
}
